home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / CPICKLECACHE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-13  |  18.3 KB  |  698 lines

  1. /*****************************************************************************
  2.   
  3.   Zope Public License (ZPL) Version 1.0
  4.   -------------------------------------
  5.   
  6.   Copyright (c) Digital Creations.  All rights reserved.
  7.   
  8.   This license has been certified as Open Source(tm).
  9.   
  10.   Redistribution and use in source and binary forms, with or without
  11.   modification, are permitted provided that the following conditions are
  12.   met:
  13.   
  14.   1. Redistributions in source code must retain the above copyright
  15.      notice, this list of conditions, and the following disclaimer.
  16.   
  17.   2. Redistributions in binary form must reproduce the above copyright
  18.      notice, this list of conditions, and the following disclaimer in
  19.      the documentation and/or other materials provided with the
  20.      distribution.
  21.   
  22.   3. Digital Creations requests that attribution be given to Zope
  23.      in any manner possible. Zope includes a "Powered by Zope"
  24.      button that is installed by default. While it is not a license
  25.      violation to remove this button, it is requested that the
  26.      attribution remain. A significant investment has been put
  27.      into Zope, and this effort will continue if the Zope community
  28.      continues to grow. This is one way to assure that growth.
  29.   
  30.   4. All advertising materials and documentation mentioning
  31.      features derived from or use of this software must display
  32.      the following acknowledgement:
  33.   
  34.        "This product includes software developed by Digital Creations
  35.        for use in the Z Object Publishing Environment
  36.        (http://www.zope.org/)."
  37.   
  38.      In the event that the product being advertised includes an
  39.      intact Zope distribution (with copyright and license included)
  40.      then this clause is waived.
  41.   
  42.   5. Names associated with Zope or Digital Creations must not be used to
  43.      endorse or promote products derived from this software without
  44.      prior written permission from Digital Creations.
  45.   
  46.   6. Modified redistributions of any form whatsoever must retain
  47.      the following acknowledgment:
  48.   
  49.        "This product includes software developed by Digital Creations
  50.        for use in the Z Object Publishing Environment
  51.        (http://www.zope.org/)."
  52.   
  53.      Intact (re-)distributions of any official Zope release do not
  54.      require an external acknowledgement.
  55.   
  56.   7. Modifications are encouraged but must be packaged separately as
  57.      patches to official Zope releases.  Distributions that do not
  58.      clearly separate the patches from the original work must be clearly
  59.      labeled as unofficial distributions.  Modifications which do not
  60.      carry the name Zope may be packaged in any form, as long as they
  61.      conform to all of the clauses above.
  62.   
  63.   
  64.   Disclaimer
  65.   
  66.     THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  67.     EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  68.     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  69.     PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  70.     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  71.     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  72.     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  73.     USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  74.     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  75.     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  76.     OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  77.     SUCH DAMAGE.
  78.   
  79.   
  80.   This software consists of contributions made by Digital Creations and
  81.   many individuals on behalf of Digital Creations.  Specific
  82.   attributions are listed in the accompanying credits file.
  83.   
  84.  ****************************************************************************/
  85. static char *what_string = "$Id: cPickleCache.c,v 1.31 1999/10/13 12:35:19 jim Exp $";
  86.  
  87. #define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;}
  88. #define UNLESS(E) if(!(E))
  89. #define UNLESS_ASSIGN(V,E) ASSIGN(V,E) UNLESS(V)
  90. #define OBJECT(O) ((PyObject*)O)
  91.  
  92. #include "cPersistence.h"
  93. #include <time.h>
  94.  
  95. #undef Py_FindMethod
  96.  
  97. static PyObject *py_reload, *py__p_jar, *py__p_changed;
  98.  
  99. typedef struct {
  100.   PyObject_HEAD
  101.   PyObject *data;
  102.   PyObject *jar;
  103.   PyObject *setklassstate;
  104.   int position;
  105.   int cache_size;
  106.   int cache_age;
  107.   /* Cache statistics */
  108.   int sum_deal;
  109.   int sum_deac;
  110.   double sum_age;
  111.   int n, na;
  112.   time_t last_check;        /* Time of last gc */
  113.   double mean_age;
  114.   double mean_deal;
  115.   double mean_deac;
  116.   double df, dfa;            /* Degees of freedom for above stats */
  117. } ccobject;
  118.  
  119. #define WEIGHTING_PERIOD 600
  120.  
  121. /*
  122.   How to compute weighted means?
  123.  
  124.   Assume we have two means, a current mean, M, and a mean as of some
  125.   time d seconds in the past, Md.  The means have effective degrees
  126.   of freedom, N, and Nd. Where Nd is adjusted by d is some fashion.
  127.   The combined mean is (M*N+Md*Nd)/(N+Nd).  The degrees of freedom
  128.   of the combined mean, Nc, is N+Nd.  Nd is computed by weighting
  129.   an old degree of freedom with the weight: I/(I+d), where I is some
  130.   suitably chosen constant, which we will call a "weighting period".
  131.   
  132.  */
  133.  
  134. staticforward PyTypeObject Cctype;
  135.  
  136. /* ---------------------------------------------------------------- */
  137.  
  138. static int 
  139. gc_item(ccobject *self, PyObject *key, PyObject *v, long now, int dt)
  140. {
  141.  
  142.   if (v && key)
  143.     {
  144.       self->n++;
  145.       if(v->ob_refcnt <= 1)
  146.     {
  147.       self->sum_deal++;
  148.       return PyDict_DelItem(self->data, key);
  149.     }
  150.  
  151.       if (dt && 
  152.       (! PyExtensionClass_Check(v)) &&
  153.       ((cPersistentObject*)v)->jar==self->jar /* I'm paranoid */ &&
  154.       ((cPersistentObject*)v)->state==cPersistent_UPTODATE_STATE
  155.       )
  156.     {
  157.       now -= ((cPersistentObject*)v)->atime;
  158.       if (now < 0) now += 65536;
  159.       self->na++;
  160.       self->sum_age += now;
  161.       if (now > dt)
  162.         {
  163.           /* We have a cPersistent object that hasn't been used in
  164.          a while.  Reinitialize it, hopefully freeing it's
  165.          state.
  166.           */
  167.           self->sum_deac++;
  168.           if (PyObject_SetAttr(v,py__p_changed,Py_None) < 0)
  169.         PyErr_Clear();
  170.         }
  171.     }
  172.     }
  173.   return 0;
  174. }
  175.  
  176. static void
  177. update_stats(ccobject *self, time_t now)
  178. {
  179.   double d, deal, deac;
  180.  
  181.   d=now-self->last_check;
  182.   if(d < 1) return;
  183.  
  184.   self->df  *= WEIGHTING_PERIOD/(WEIGHTING_PERIOD+d);
  185.   self->dfa *= WEIGHTING_PERIOD/(WEIGHTING_PERIOD+d);
  186.  
  187.   self->mean_age=((self->mean_age*self->dfa+self->sum_age)/
  188.           (self->dfa+self->na))*3;
  189.   self->sum_age=0;
  190.  
  191.   deac=self->sum_deac/d;
  192.   self->sum_deac=0;
  193.   self->mean_deac=((self->mean_deac*self->dfa+deac)/
  194.            (self->dfa+self->na));
  195.   self->sum_deac=0;
  196.  
  197.   self->dfa += self->na;
  198.   self->na=0;
  199.  
  200.   deal=self->sum_deal/d;
  201.   self->sum_deal=0;
  202.   self->mean_deal=((self->mean_deal*self->df +deal)/
  203.            (self->df +self->n));
  204.   self->sum_deal=0;
  205.  
  206.   self->df += self->n;
  207.   self->n=0;
  208.  
  209.   self->last_check=now;
  210. }
  211.  
  212. static int
  213. fullgc(ccobject *self, int dt)
  214. {
  215.   PyObject *key, *v;
  216.   int i;
  217.   long now;
  218.  
  219.   if (self->cache_size < 1) return 0;
  220.   if ((i=PyDict_Size(self->data)) < 1) return 0;
  221.  
  222.   now=((long)(time(NULL)/3))%65536;
  223.   if (dt < 0) dt=0;
  224.   else dt /= 3;
  225.  
  226.   for(i=0; PyDict_Next(self->data, &i, &key, &v); )
  227.     if(gc_item(self,key,v,now,dt) < 0) return -1;
  228.   self->position=0;
  229.  
  230.   if(now-self->last_check > 1) update_stats(self, now);
  231.   
  232.   return 0;
  233. }
  234.  
  235. static int
  236. reallyfullgc(ccobject *self, int dt)
  237. {
  238.   PyObject *key, *v;
  239.   int i, l, last;
  240.   time_t now;
  241.  
  242.   if (self->cache_size < 1) return 0;
  243.   if((last=PyDict_Size(self->data)) < 0) return -1;
  244.  
  245.   now=((long)(time(NULL)/3))%65536;
  246.   if (dt < 0) dt=0;
  247.   else dt /= 3;
  248.  
  249.   /* First time through should get refcounts to 1 */
  250.   for(i=0; PyDict_Next(self->data, &i, &key, &v); )
  251.     if(gc_item(self,key,v,now,dt) < 0) return -1;
  252.  
  253.   if((l=PyDict_Size(self->data)) < 0) return -1;
  254.   while(l < last)
  255.     {
  256.       for(i=0; PyDict_Next(self->data, &i, &key, &v); )
  257.     if(gc_item(self,key,v,now,dt) < 0) return -1;
  258.       last=l;
  259.       if((l=PyDict_Size(self->data)) < 0) return -1;
  260.     }
  261.  
  262.   if(now-self->last_check > 1) update_stats(self, now);
  263.  
  264.   self->position=0;
  265.   return 0;
  266. }
  267.  
  268. static int
  269. maybegc(ccobject *self, PyObject *thisv)
  270. {
  271.   int n, s, size, dt;
  272.   long now;
  273.   PyObject *key=0, *v=0;
  274.  
  275.   if (self->cache_size < 1) return 0;
  276.   s=PyDict_Size(self->data);
  277.   if (s < 1) return s;
  278.  
  279.   now=((long)(time(NULL)/3))%65536;
  280.  
  281.   size=self->cache_size;
  282.   self->cache_size=0;
  283.  
  284.   /* Decide how many objects to look at */
  285.   n=(s-size)/10;
  286.   if (n < 3) n=3;
  287.  
  288.   /* Decide how much time to give them before deactivating them */
  289.   s=8*size/s;
  290.   if (s > 100) s=100;
  291.   dt=(long)(self->cache_age*(0.2+0.1*s));
  292.  
  293.   /* Units are 3 seconds */
  294.   dt /= 3; 
  295.  
  296.   if (dt < 1) dt=1;
  297.   
  298.   while (--n >= 0)
  299.     {
  300.       if (PyDict_Next(self->data, &(self->position), &key, &v))
  301.     {
  302.       if (v != thisv && gc_item(self,key,v,now,dt) < 0)
  303.         {
  304.           self->cache_size=size;
  305.           return -1;
  306.         }
  307.     }
  308.       else
  309.     self->position=0;
  310.     }
  311.   self->cache_size=size;
  312.  
  313.   if (now-self->last_check > 1) update_stats(self, now);
  314.  
  315.   return 0;
  316. }
  317.  
  318. static PyObject *
  319. cc_full_sweep(ccobject *self, PyObject *args)
  320. {
  321.   int dt=0;
  322.   UNLESS(PyArg_ParseTuple(args, "|i", &dt)) return NULL;
  323.   UNLESS(-1 != fullgc(self,dt)) return NULL;
  324.   Py_INCREF(Py_None);
  325.   return Py_None;
  326. }
  327.  
  328. static PyObject *
  329. cc_reallyfull_sweep(ccobject *self, PyObject *args)
  330. {
  331.   int dt=0;
  332.   UNLESS(PyArg_ParseTuple(args, "|i", &dt)) return NULL;
  333.   UNLESS(-1 != reallyfullgc(self,dt)) return NULL;
  334.   Py_INCREF(Py_None);
  335.   return Py_None;
  336. }
  337.  
  338. static PyObject *
  339. cc_incrgc(ccobject *self, PyObject *args)
  340. {
  341.   int n=1;
  342.  
  343.   UNLESS (PyArg_ParseTuple(args, "|i",&n)) return NULL;
  344.  
  345.   for (; --n >= 0;)
  346.     if(maybegc(self,NULL) < 0) return NULL;
  347.  
  348.   Py_INCREF(Py_None);
  349.   return Py_None;
  350. }
  351.  
  352. static void 
  353. _invalidate(ccobject *self, PyObject *key)
  354. {
  355.   PyObject *v;
  356.  
  357.   if ((v=PyDict_GetItem(self->data, key)))
  358.     {
  359.       if (PyExtensionClass_Check(v))
  360.     if(v->ob_refcnt <= 1)
  361.       {
  362.         self->sum_deal++;
  363.         if (PyDict_DelItem(self->data, key) < 0) 
  364.           PyErr_Clear();
  365.       }
  366.     else
  367.       {
  368.         v=PyObject_CallFunction(self->setklassstate,
  369.                     "O", v);
  370.         if (v) Py_DECREF(v);
  371.         else PyErr_Clear();
  372.       }
  373.       else if (PyObject_DelAttr(v,py__p_changed) < 0)
  374.     PyErr_Clear();
  375.     }
  376.   else PyErr_Clear();
  377. }
  378.  
  379. static PyObject *
  380. cc_invalidate(ccobject *self, PyObject *args)
  381. {
  382.   PyObject *inv, *key, *v;
  383.   int i;
  384.   
  385.   if (PyArg_ParseTuple(args, "O!", &PyDict_Type, &inv)) {
  386.     for (i=0; PyDict_Next(inv, &i, &key, &v); ) 
  387.       if (key==Py_None)
  388.     { /* Eek some nitwit invalidated everything! */
  389.       for (i=0; PyDict_Next(self->data, &i, &key, &v); )
  390.         _invalidate(self, key);
  391.       break;
  392.     }
  393.       else
  394.     _invalidate(self, key);
  395.     PyDict_Clear(inv);
  396.   }
  397.   else {
  398.     PyErr_Clear();
  399.     UNLESS (PyArg_ParseTuple(args, "O", &inv)) return NULL;
  400.     if (PyString_Check(inv))
  401.       _invalidate(self, inv);
  402.     else if (inv==Py_None)    /* All */
  403.       for (i=0; PyDict_Next(self->data, &i, &key, &v); )
  404.     _invalidate(self, key);
  405.     else {
  406.       int l;
  407.  
  408.       PyErr_Clear();
  409.       if ((l=PyObject_Length(inv)) < 0) return NULL;
  410.       for(i=l; --i >= 0; )
  411.     {
  412.       UNLESS (key=PySequence_GetItem(inv, i)) return NULL;
  413.       _invalidate(self, key);
  414.       Py_DECREF(key);
  415.     }
  416.       PySequence_DelSlice(inv, 0, l);
  417.     }
  418.   }
  419.  
  420.   Py_INCREF(Py_None);
  421.   return Py_None;
  422. }
  423.   
  424.   
  425. static PyObject *
  426. cc_get(ccobject *self, PyObject *args)
  427. {
  428.   PyObject *r, *key, *d=0;
  429.  
  430.   UNLESS (PyArg_ParseTuple(args,"O|O", &key, &d)) return NULL;
  431.  
  432.   UNLESS (r=PyDict_GetItem(self->data, key))
  433.     {
  434.       if (d) 
  435.     {
  436.       PyErr_Clear();
  437.       r=d;
  438.     }
  439.       else
  440.     {
  441.       PyErr_SetObject(PyExc_KeyError, key);
  442.       return NULL;
  443.     }
  444.     }
  445.  
  446.   Py_INCREF(r);
  447.   return r;
  448. }
  449.  
  450.  
  451. static struct PyMethodDef cc_methods[] = {
  452.   {"full_sweep", (PyCFunction)cc_full_sweep, METH_VARARGS,
  453.    "full_sweep([age]) -- Perform a full sweep of the cache\n\n"
  454.    "Make a single pass through the cache, removing any objects that are no\n"
  455.    "longer referenced, and deactivating objects that have not been\n"
  456.    "accessed in the number of seconds given by 'age'.  "
  457.    "'age defaults to the cache age.\n"
  458.    },
  459.   {"minimize",    (PyCFunction)cc_reallyfull_sweep, METH_VARARGS,
  460.    "minimize([age]) -- Remove as many objects as possible\n\n"
  461.    "Make multiple passes through the cache, removing any objects that are no\n"
  462.    "longer referenced, and deactivating objects that have not been\n"
  463.    "accessed in the number of seconds given by 'age'.  'age defaults to 0.\n"
  464.    },
  465.   {"incrgc", (PyCFunction)cc_incrgc, METH_VARARGS,
  466.    "incrgc() -- Perform incremental garbage collection"},
  467.   {"invalidate", (PyCFunction)cc_invalidate, METH_VARARGS,
  468.    "invalidate(oids) -- invalidate one, many, or all ids"},
  469.   {"get", (PyCFunction)cc_get, METH_VARARGS,
  470.    "get(key [, default]) -- get an item, or a default"},
  471.   {NULL,        NULL}        /* sentinel */
  472. };
  473.  
  474. static ccobject *
  475. newccobject(PyObject *jar, int cache_size, int cache_age)
  476. {
  477.   ccobject *self;
  478.   
  479.   UNLESS(self = PyObject_NEW(ccobject, &Cctype)) return NULL;
  480.   self->setklassstate=self->jar=NULL;
  481.   if(self->data=PyDict_New())
  482.     {
  483.       self->jar=jar; 
  484.       Py_INCREF(jar);
  485.       UNLESS (self->setklassstate=PyObject_GetAttrString(jar, "setklassstate"))
  486.     return NULL;
  487.       self->position=0;
  488.       self->cache_size=cache_size;
  489.       self->cache_age=cache_age < 1 ? 1 : cache_age;
  490.       self->sum_deal=0;
  491.       self->sum_deac=0;
  492.       self->sum_age=0;
  493.       self->mean_deal=0;
  494.       self->mean_deac=0;
  495.       self->mean_age=0;
  496.       self->df=1;
  497.       self->dfa=1;
  498.       self->n=0;
  499.       self->na=0;
  500.       self->last_check=time(NULL);
  501.       return self;
  502.     }
  503.   Py_DECREF(self);
  504.   return NULL;
  505. }
  506.  
  507. static void
  508. cc_dealloc(ccobject *self)
  509. {
  510.   Py_XDECREF(self->data);
  511.   Py_XDECREF(self->jar);
  512.   Py_XDECREF(self->setklassstate);
  513.   PyMem_DEL(self);
  514. }
  515.  
  516. static PyObject *
  517. cc_getattr(ccobject *self, char *name)
  518. {
  519.   PyObject *r;
  520.  
  521.   if(*name=='c')
  522.     {
  523.       if(strcmp(name,"cache_age")==0)
  524.     return PyInt_FromLong(self->cache_age);
  525.       if(strcmp(name,"cache_size")==0)
  526.     return PyInt_FromLong(self->cache_size);
  527.       if(strcmp(name,"cache_mean_age")==0)
  528.     return PyFloat_FromDouble(self->mean_age);
  529.       if(strcmp(name,"cache_mean_deal")==0)
  530.     return PyFloat_FromDouble(self->mean_deal);
  531.       if(strcmp(name,"cache_mean_deac")==0)
  532.     return PyFloat_FromDouble(self->mean_deac);
  533.       if(strcmp(name,"cache_df")==0)
  534.     return PyFloat_FromDouble(self->df);
  535.       if(strcmp(name,"cache_dfa")==0)
  536.     return PyFloat_FromDouble(self->dfa);
  537.       if(strcmp(name,"cache_last_gc_time")==0)
  538.     return PyFloat_FromDouble(self->last_check);
  539.       if(strcmp(name,"cache_data")==0)
  540.     {
  541.       Py_INCREF(self->data);
  542.       return self->data;
  543.     }
  544.     }
  545.   if(
  546.      *name=='h' && strcmp(name, "has_key")==0 ||
  547.      *name=='i' && strcmp(name, "items")==0 ||
  548.      *name=='k' && strcmp(name, "keys")==0
  549.      )
  550.     return PyObject_GetAttrString(self->data, name);
  551.  
  552.   if(r=Py_FindMethod(cc_methods, (PyObject *)self, name))
  553.     return r;
  554.   PyErr_Clear();
  555.   return PyObject_GetAttrString(self->data, name);
  556. }
  557.  
  558. static int
  559. cc_setattr(ccobject *self, char *name, PyObject *value)
  560. {
  561.   if(value)
  562.     {
  563.       int v;
  564.  
  565.       if(strcmp(name,"cache_age")==0)
  566.     {
  567.       UNLESS(PyArg_Parse(value,"i",&v)) return -1;
  568.       if(v > 0)self->cache_age=v;
  569.       return 0;
  570.     }
  571.  
  572.       if(strcmp(name,"cache_size")==0)
  573.     {
  574.       UNLESS(PyArg_Parse(value,"i",&v)) return -1;
  575.       self->cache_size=v;
  576.       return 0;
  577.     }
  578.     }
  579.   PyErr_SetString(PyExc_AttributeError, name);
  580.   return -1;
  581. }
  582.  
  583. static int
  584. cc_length(ccobject *self)
  585. {
  586.   return PyObject_Length(self->data);
  587. }
  588.   
  589. static PyObject *
  590. cc_subscript(ccobject *self, PyObject *key)
  591. {
  592.   PyObject *r;
  593.  
  594.   UNLESS (r=PyDict_GetItem(self->data, key))
  595.   {
  596.     PyErr_SetObject(PyExc_KeyError, key);
  597.     return NULL;
  598.   }
  599.  
  600.   Py_INCREF(r);
  601.   return r;
  602. }
  603.  
  604. static int
  605. cc_ass_sub(ccobject *self, PyObject *key, PyObject *v)
  606. {
  607.   if(v) 
  608.     {
  609.       if (PyExtensionClass_Check(v) 
  610.       ||
  611.       (PyExtensionInstance_Check(v) 
  612.        &&
  613.        (((PyExtensionClass*)(v->ob_type))->class_flags 
  614.         & PERSISTENT_TYPE_FLAG)
  615.        &&
  616.        (v->ob_type->tp_basicsize >= sizeof(cPersistentObject))
  617.        )
  618.       )      
  619.     return PyDict_SetItem(self->data, key, v);
  620.  
  621.       PyErr_SetString(PyExc_ValueError,
  622.               "Cache values must be persistent objects or classes.");
  623.       return -1;
  624.     }
  625.   return PyDict_DelItem(self->data, key);
  626. }
  627.  
  628. static PyMappingMethods cc_as_mapping = {
  629.   (inquiry)cc_length,        /*mp_length*/
  630.   (binaryfunc)cc_subscript,    /*mp_subscript*/
  631.   (objobjargproc)cc_ass_sub,    /*mp_ass_subscript*/
  632. };
  633.  
  634. static PyTypeObject Cctype = {
  635.   PyObject_HEAD_INIT(NULL)
  636.   0,                /*ob_size*/
  637.   "cPickleCache",        /*tp_name*/
  638.   sizeof(ccobject),        /*tp_basicsize*/
  639.   0,                /*tp_itemsize*/
  640.   /* methods */
  641.   (destructor)cc_dealloc,    /*tp_dealloc*/
  642.   (printfunc)0,            /*tp_print*/
  643.   (getattrfunc)cc_getattr,    /*tp_getattr*/
  644.   (setattrfunc)cc_setattr,    /*tp_setattr*/
  645.   (cmpfunc)0,            /*tp_compare*/
  646.   (reprfunc)0,           /*tp_repr*/
  647.   0,                /*tp_as_number*/
  648.   0,                /*tp_as_sequence*/
  649.   &cc_as_mapping,        /*tp_as_mapping*/
  650.   (hashfunc)0,            /*tp_hash*/
  651.   (ternaryfunc)0,        /*tp_call*/
  652.   (reprfunc)0,          /*tp_str*/
  653.  
  654.   /* Space for future expansion */
  655.   0L,0L,0L,0L,
  656.   ""
  657. };
  658.  
  659. static PyObject *
  660. cCM_new(PyObject *self, PyObject *args)
  661. {
  662.   int cache_size=100, cache_age=1000;
  663.   PyObject *jar;
  664.  
  665.   UNLESS(PyArg_ParseTuple(args, "O|ii", &jar, &cache_size, &cache_age)) return NULL;
  666.   return (PyObject*)newccobject(jar, cache_size,cache_age);
  667. }
  668.  
  669. static struct PyMethodDef cCM_methods[] = {
  670.   {"PickleCache",(PyCFunction)cCM_new,    METH_VARARGS, ""},
  671.   {NULL,        NULL}        /* sentinel */
  672. };
  673.  
  674. void
  675. initcPickleCache()
  676. {
  677.   PyObject *m, *d;
  678.   char *rev="$Revision: 1.31 $";
  679.  
  680.   Cctype.ob_type=&PyType_Type;
  681.  
  682.   UNLESS(ExtensionClassImported) return;
  683.  
  684.   m = Py_InitModule4("cPickleCache", cCM_methods, "",
  685.              (PyObject*)NULL,PYTHON_API_VERSION);
  686.  
  687.   d = PyModule_GetDict(m);
  688.  
  689.   py_reload=PyString_FromString("reload");
  690.   py__p_jar=PyString_FromString("_p_jar");
  691.   py__p_changed=PyString_FromString("_p_changed");
  692.  
  693.   PyDict_SetItemString(d,"__version__",
  694.                PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));
  695.   
  696.   if (PyErr_Occurred()) Py_FatalError("can't initialize module cCache");
  697. }
  698.